/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buttonOptions;

import constants.GameControllerServerConstants;
import controllerButtons.StaticControllerButton;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.colorchooser.DefaultColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author luc0
 */
public class StaticControllerOptionFrame extends JFrame {

    private MouseEvent mouseEvent;
    private JLayeredPane controllerPanel;
    private StaticControllerButton controllerButton;
    private JPanel optionPanel;
    private JButton saveButton;
    private JLabel buttonUpLabel, buttonDownLabel, buttonRightLabel, buttonLeftLabel;
    private JComboBox buttonUpComboBox, buttonDownComboBox, buttonRightComboBox, buttonLeftComboBox;
    private JColorChooser colorChooser;
    public StaticControllerOptionFrame(MouseEvent evt,JLayeredPane controllerPanel) {
        super();
        this.mouseEvent = evt;
        this.controllerPanel =controllerPanel;
        
        this.controllerButton = (StaticControllerButton) evt.getSource();
        optionPanel = new JPanel();

        this.setSize(600, 500);
        this.setLocation(evt.getLocationOnScreen().x, evt.getLocationOnScreen().y);
        optionPanel.setSize(this.getSize());


        //Option Panel
        optionPanel.setLayout(new GridBagLayout());
        final GridBagConstraints c = new GridBagConstraints();

        //Labels
        buttonUpLabel = new JLabel("Up:");
        buttonDownLabel = new JLabel("Down:");
        buttonRightLabel = new JLabel("Right:");
        buttonLeftLabel = new JLabel("Left:");


        //ComboBoxes
        buttonUpComboBox = new JComboBox(GameControllerServerConstants.AVAILABLE_KEYS);
        buttonUpComboBox.setSelectedItem(controllerButton.getUpButton());

        buttonDownComboBox = new JComboBox(GameControllerServerConstants.AVAILABLE_KEYS);
        buttonDownComboBox.setSelectedItem(controllerButton.getDownButton());

        buttonRightComboBox = new JComboBox(GameControllerServerConstants.AVAILABLE_KEYS);
        buttonRightComboBox.setSelectedItem(controllerButton.getRightButton());


        buttonLeftComboBox = new JComboBox(GameControllerServerConstants.AVAILABLE_KEYS);
        buttonLeftComboBox.setSelectedItem(controllerButton.getLeftButton());


        saveButton = new JButton("Save");
        saveButton.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                onSaveButtonClicked(e);
            }

            public void mousePressed(MouseEvent e) {
                //  throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
                //  throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                //  throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }
        });

       
        //Color chooser
        //Color Chooser
        colorChooser = new JColorChooser();
        colorChooser.setPreviewPanel(new JPanel());
        colorChooser.setSelectionModel(new DefaultColorSelectionModel());
        colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                Color newColor = colorChooser.getColor();
                controllerButton.setColor(newColor);
                controllerButton.repaint();
            }
        });
        
        //Row 1
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        optionPanel.add(buttonUpLabel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        optionPanel.add(buttonUpComboBox, c);

        //Row 2
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        optionPanel.add(buttonDownLabel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        optionPanel.add(buttonDownComboBox, c);

        //Row 3
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        optionPanel.add(buttonRightLabel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        optionPanel.add(buttonRightComboBox, c);

        //Row 4
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        optionPanel.add(buttonLeftLabel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        optionPanel.add(buttonLeftComboBox, c);

        //Row 5
         c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        optionPanel.add(colorChooser, c);


        //Row 6
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 5;
        optionPanel.add(saveButton, c);

        
        this.add(optionPanel);
    }

    private void onSaveButtonClicked(MouseEvent evt) {
        GameControllerServerConstants.isOptionInUse = false;
        controllerButton.setUpButton((String)buttonUpComboBox.getSelectedItem());
        controllerButton.setDownButton((String)buttonDownComboBox.getSelectedItem());
        controllerButton.setLeftButton((String)buttonLeftComboBox.getSelectedItem());
        controllerButton.setRightButton((String)buttonRightComboBox.getSelectedItem());
        dispose();
    }

}
